<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/01/26
 * Time: 下午 10:36
 */

namespace app\admin\model;

use think\Model;

/**
 * Class Sysmsgabout
 * @package app\admin\model
 * 消息-模型
 */
class Sysmsgabout extends Model {

    public function doModifyabout($data) {
        $id = $data['id'];
        $data['update_time'] = time();
        $data['content'] = $data['contentt'];
        unset($data['contentt']);
        if ($id == '') {
            $data['is_about'] = 1;
            $result = db("Sysmsg_about")
                    ->insert($data);
        } else {
            unset($data['id']);
            $result = db("Sysmsg_about")
                    ->where('id', $id)
                    ->update($data);
        }

        if (!$result) {
            exception('修改失败');
        }
        return $result;
    }

    /**
     * @param $is_about
     * @param $pageNum
     * @param $numPerPage
     * @return mixed
     * 消息-列表
     */
    public function getSysList($is_about, $pageNum, $numPerPage, $title) {
        $map['is_about'] = $is_about;
        $map['is_delete'] = 0;
        if (!empty($title)) {
            $map['title'] = ['like', "%{$title}%"];
        }
        $list = db("Sysmsg_about")
                ->where($map)
                ->page($pageNum, $numPerPage)
                ->order('update_time desc')
                ->select();
        return $list;
    }

    /**
     * @param $is_about
     * @return mixed
     * 消息-数量
     */
    public function getSysCount($is_about, $title) {
        $map['is_about'] = $is_about;
        $map['is_delete'] = 0;
        if (!empty($title)) {
            $map['title'] = ['like', "%{$title}%"];
        }
        $count = db("Sysmsg_about")
                ->field("id")
                ->where($map)
                ->count();
        return $count;
    }

    /**
     * @param $data
     * @return int|string
     * @throws \Exception
     * 消息-添加
     */
    public function doAddSys($data) {
        $data['create_time'] = time();
        $data['update_time'] = time();
        $data['is_delete'] = 0;
        if ($data['is_about'] == 1) {
            db("Sysmsg_about")->where('is_about', 1)->update(['is_delete' => 1]);
        }
        $result = db("Sysmsg_about")->insert($data);
        if (!$result) {
            exception('添加失败');
        }
        return $result;
    }

    /**
     * @param $data
     * @return int|string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     * 消息-修改
     */
    public function doModifySys($data) {
        $id = $data['id'];
        unset($data['id']);
        if ($data['is_about'] == 1) {
            db("Sysmsg_about")->where('id', 'neq', $id)->where('is_about', 1)->update(['is_delete' => 1]);
        }
        $data['update_time'] = time();
        $result = db("Sysmsg_about")->where('id', $id)->update($data);
        if (!$result) {
            exception('修改失败');
        }
        return $result;
    }

    /**
     * @param $id
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     * 消息-删除
     */
    public function delSys($id) {
        $one = db('Sysmsg_about')
                ->where('id', 'neq', $id)
                ->where('is_about', 1)
                ->where('is_delete', 0)
                ->count();
        if ($one == 0) {
            exception('请至少保留一条消息');
        }
//        $data['update_time'] = time();
//        $data['is_delete'] = 1;
        $del = db("Sysmsg_about")
                ->where("id", $id)
                ->delete();
        if (!$del) {
            exception('删除失败');
        }
        return $del;
    }

    /**
     * @Desc 更新信息修改
     * @param $data
     *
     * @return int|string
     */
    public function doModifyUpdate($data)
    {
        $id = $data['id'];
        $data['create_time'] = strtotime($data['create_time']);
        $data['update_time'] = strtotime($data['update_time']);
        if ($id == '') {
            $data['is_about'] = 2;
            $result = db("Sysmsg_about")
                ->insert($data);
        } else {
            unset($data['id']);
            $result = db("Sysmsg_about")
                ->where('id', $id)
                ->update($data);
        }

        if (!$result) {
            exception('修改失败');
        }
        return $result;
    }
}
